@echo off
echo ========================================
echo Oracle Viewer 项目构建脚本
echo ========================================

echo.
echo 检查Java环境...
java -version
if %errorlevel% neq 0 (
    echo 错误: 未找到Java环境，请先安装Java 8或更高版本
    pause
    exit /b 1
)

echo.
echo 检查Maven环境...
mvn -version
if %errorlevel% neq 0 (
    echo 警告: 未找到Maven环境
    echo 正在下载Maven Wrapper...
    
    if not exist "mvnw.cmd" (
        echo 创建Maven Wrapper...
        powershell -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/takari/maven-wrapper/master/mvnw.cmd' -OutFile 'mvnw.cmd'"
        powershell -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/takari/maven-wrapper/master/mvnw' -OutFile 'mvnw'"
        if not exist ".mvn\wrapper" mkdir ".mvn\wrapper"
        powershell -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/takari/maven-wrapper/master/.mvn/wrapper/maven-wrapper.properties' -OutFile '.mvn\wrapper\maven-wrapper.properties'"
        powershell -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/takari/maven-wrapper/master/.mvn/wrapper/maven-wrapper.jar' -OutFile '.mvn\wrapper\maven-wrapper.jar'"
    )
    
    set MAVEN_CMD=mvnw.cmd
) else (
    set MAVEN_CMD=mvn
)

echo.
echo 使用Maven命令: %MAVEN_CMD%

echo.
echo 清理项目...
%MAVEN_CMD% clean

echo.
echo 编译项目...
%MAVEN_CMD% compile

if %errorlevel% neq 0 (
    echo 编译失败！
    pause
    exit /b 1
)

echo.
echo 编译成功！

echo.
echo 选择操作:
echo 1. 运行项目
echo 2. 打包成JAR
echo 3. 退出

set /p choice=请输入选择 (1-3): 

if "%choice%"=="1" (
    echo.
    echo 启动项目...
    %MAVEN_CMD% spring-boot:run
) else if "%choice%"=="2" (
    echo.
    echo 打包项目...
    %MAVEN_CMD% package
    if %errorlevel% equ 0 (
        echo.
        echo 打包成功！JAR文件位置: target\oracle-viewer-1.0.0.jar
        echo.
        echo 运行JAR文件:
        echo java -jar target\oracle-viewer-1.0.0.jar
    )
) else (
    echo 退出
)

pause 